package com.novel.controller.novel;


import com.novel.common.exception.CustomerException;
import com.novel.controller.other.BaseController;
import com.novel.pojo.novel.NovelReward;
import com.novel.pojo.system.User;
import com.novel.service.NovelRewardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Controller
@RequestMapping("novel/reward")
public class NovelRewardController extends BaseController {

    @Autowired
    private NovelRewardService novelRewardService;

    /**
     * 小说打赏
     */
    @PostMapping("{novelId}")
    public String rewardNovel(@PathVariable("novelId") String novelId, Double cost) {

        User user = getUserInformation();

        if (Objects.isNull(user)) {
            return "redirect:/login";
        }

        NovelReward reward = new NovelReward();
        reward.setRewardId(UUID.randomUUID().toString());
        reward.setActionIp(getUserIpAddress());
        reward.setActionTime(new Date());
        reward.setRewardMoney(cost);
        reward.setNovelId(novelId);
        reward.setUserId(user.getUserId());

        if (novelRewardService.rewardNovelByNovelId(reward)) {
            return "redirect:/chapter/info/" + novelId;
        }
        throw new CustomerException("打赏失败!");
    }

}